<template>
	<view>
	  <view class="title"></view>
	  <view class="list">
	    <view class="list-item">
	      <view class="left"><text class="letter-spacing">手机</text>号</view>
	      <input class="right" type="number" @input="onKeyInput" id="mobile" placeholder="请输入手机号" maxlength="11"/>
	    </view>
			
			<view class="list-item" style="padding-right: 32rpx;">
				<view class="left"><text class="letter-spacing">验证</text>码</view>
				<input class="right" @input="onKeyInput" id="verifyCode" type="number" placeholder="请输入验证码" maxlength="6"/>
				<button class="getverifyCode" :disabled="mobileDisabled" @click="getCode">{{btnName}}</button>
			</view>
	  </view>
	  <view class="btnBox">
	    <button class="btn" type="primary" :disabled="disabled" @click="nextBtn">登录</button>
	  </view>
	</view>
</template>

<script>
	import {getSMSCode, toLogin, scToLogin} from '@/service/login'
	export default {
		data() {
			return {
				mobile: '',
				verifyCode: '',
				btnName: '获取验证码',
				userInfo: {},
				disabled: false,
				mobileDisabled: false,
				skip: "",
				auth_code: ''
			}
		},
		onLoad(e) {
			this.auth_code = e.auth_code;
			/**↓↓↓其他页面进入，登录成功后要跳转的页面 */
			if(e.id){
				this.skip = e.id;
			}
		},
		onShow() {
			let userInfoData = uni.getStorageSync('userInfo');
			if(userInfoData.token != null ) {
				uni.switchTab({ url: '/pages/home/home'})
			}
		},
		methods: {
			onKeyInput(e) {
				let typeName = e.currentTarget.id;
				this[typeName] = e.detail.value;
			},
			skipFun(thisId){
				let dealerId = ''
				if(thisId == '' || thisId == undefined || thisId == null){
					uni.switchTab({url: '../home/home'});
					return
				}
				thisId = thisId * 1;
				uni.getStorage({
					key: 'dealerId',
					success: function(res) {
						dealerId = res.data.dealerId;
					}
				});
				switch (thisId) {
					case 0:
						getApp().globalData.bizType = 2;
						uni.switchTab({
							url: '../carList/carList'
						}); // 商城-选车
						break;
					case 3:
						getApp().globalData.bizType = 3;
						if (dealerId == 0) {
							uni.navigateTo({
								url: '../carshop/special/special'
							}); /**留资-活动详情 */
						}else{
							uni.navigateTo({
								url: '/chezhudai/pages/plan/plan'
							});
						}
						break;
					case 1:
						getApp().globalData.bizType = 1;
						uni.navigateTo({
							url: '../carshop/discover/discover?typeId='+thisId
						}); // 商城-汽车保养
						break;
					case 2:
						getApp().globalData.bizType = 1;
						uni.navigateTo({
							url: '../carshop/discover/discover?typeId='+thisId
						}); // 商城-汽车美容
						break;
					case 6:
						getApp().globalData.bizType = 6;
						if (dealerId == 0) {
							uni.navigateTo({
								url: '../carshop/special-offer/special-offer'
							}); /**留资-活动 */
						} else {
							uni.navigateTo({
								url: '../initial/plan/plan'
							}); // 车抵贷-选择方案
						}
						break;
					case 10:
						getApp().globalData.bizType = 10;
						uni.navigateTo({
							url: '../../housePurchase/index/index'
						}); // 选房购房
						break;
					case 15:
						uni.switchTab({
							url: '../profile/profile'
						}); /**我的 */
						break;
					default:
						uni.switchTab({url: '../home/home'});
						break;
				}
			},
			/**
			 * 获取验证码
			 * @method getCode
			 */
			getCode() {
				const mobile = this.mobile;
				if(mobile == "") {
					uni.showToast({
						icon: 'none',
						title: '请输入手机号',
						duration: 3000
					});
				}else if(!(/^1[3456789]\d{9}$/.test(mobile))) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的手机号',
						duration: 3000
					});
				}else {
					const prams = {mobile, type: 'login'};
					this._getSMSCode(JSON.stringify(prams));
				}
			},
			/**
			 * 倒计时定时器
			 * @method settimer
			 */
			settimer() {
				uni.hideLoading();
				let timer;
				let codeTime = 60;
				this.mobileDisabled = true;
				clearInterval(timer);
				timer = setInterval(() => {
					codeTime--;
					this.btnName = `倒计时${codeTime}秒`;
					if (codeTime === 0) {
						clearInterval(timer);
						this.btnName = '重新发送';
						this.mobileDisabled = false;
					}
				}, 1000);
			},
			nextBtn() {
				const mobile = this.mobile;
				const verifyCode = this.verifyCode;
				if(mobile == "") {
					uni.showToast({
						icon: 'none',
						title: '请输入手机号',
						duration: 3000
					});
				}else if(!(/^1[3456789]\d{9}$/.test(mobile))) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的手机号',
						duration: 3000
					});
				}else if(verifyCode == "") {
					uni.showToast({
						icon: 'none',
						title: '请输入验证码',
						duration: 3000
					});
				}else {
					let mobile = this.mobile;
					let smsCode = this.verifyCode;
					let saveCookid = 'y';
					let code = this.auth_code;
					let prams = {mobile, smsCode, saveCookid, code}
					let pram = {mobile};
					this._toLogin(JSON.stringify(prams));
					this._scToLogin(JSON.stringify(pram));
				}
			},
			// -----------------------网络请求--------------------------
			_getSMSCode(prams) {
				let _this = this;
				getSMSCode(prams).then(res => {
					const data = res.data;
					if(data.resultCode != "0000") {
						uni.showModal({
							showCancel: false,
							content: res.data.resultMsg
						});
					}else {
						this.settimer();
					}
				}).catch(err => {
					uni.hideLoading();
				})
			},
			_toLogin(prams) {
				let _this = this;
				toLogin(prams).then(res => {
					const data = res.data;
					if(data.resultCode != "0000") {
						uni.showModal({
							showCancel: false,
							content: data.resultMsg
						});
					}else {
						let userInfo = data.data;
						let globalData = getApp().globalData;
						let extraData = globalData.extraData;
						// let userInfoData = uni.getStorageSync('userInfo');
						let userInfoData = {};
						userInfoData.mobile = this.mobile;
						userInfoData.userId = 'fbca3bffd23a11eb8bf398039b285ed0';
						userInfoData.token = userInfo.token;
						// userInfoData.siteUserId = "2088912568317341";
						userInfoData.siteUserId = "2088802101703134";
						globalData.mobile = this.mobile;
						globalData.userId = 'fbca3bffd23a11eb8bf398039b285ed0';
						globalData.token = userInfo.token;
						// globalData.siteUserId = "2088912568317341";
						globalData.siteUserId = "2088802101703134"; 
						// extraData = userInfoData;
						getApp().globalData.extraData.mobile = this.mobile;
						getApp().globalData.extraData.userId = 'fbca3bffd23a11eb8bf398039b285ed0';
						getApp().globalData.extraData.token = userInfo.token;
						uni.setStorage({
							key: 'userInfo',
							data: userInfoData,
							success: function(res) {
								uni.showModal({
									showCancel: false,
									content: '登录成功',
									success: () => {
										// uni.switchTab({ url: '/pages/home/home'})
										_this.skipFun(_this.skip);
									}
								});
							}
						})
						
					}
				}).catch(err => {
					uni.hideLoading();
				})
			},
			/**商城登录接口 */
			_scToLogin(prams){
				scToLogin(prams).then(res => {
					let data = res.data;
					if (data.resultCode != '0000') {
						uni.showModal({
							title: '提示',
							content: data.resultMsg,
							showCancel: true
						})
					} else {
						uni.setStorage({
							key: 'scUserInfo',
							data: data,
							success: res => {
								// console.log("res", res);
							},
							fail: err => {
								// console.log("err", err);
							}
						})
					}
				}).catch(err => {
					// console.log("err", err);
				})
			},

			_toAutoLogin(prams) {
				let _this = this;
				toLogin(prams).then(res => {
					const data = res.data;
					if(data.resultCode == "0000") {
						const userInfo = data.data;
						this.userInfo = userInfo;
						if(userInfo.status != 1) {
							uni.removeStorage({
								key: 'userInfo',
								success: function(){
									uni.removeStorage({
										key: 'userData',
										success: function(){}
									});
								}
							});
						}else {
							uni.setStorage({
								key: 'userInfo',
								data: userInfo,
								success: function() {
									uni.navigateTo({ url: '../index/index' }); 
								}
							})
						}
					}else {
						uni.removeStorage({
							key: 'userInfo',
							success: function(){
								uni.removeStorage({
									key: 'userData',
									success: function(){}
								});
							}
						});
					}
				}).catch(err => {
					uni.hideLoading();
				})
			},
		}
	}
</script>

<style>
	.title {
	  display: flex;
	  padding: 32rpx;
	}
	
	.title-point {
	  width: 24rpx;
	  height: 24rpx;
	  border-radius: 50%;
	  background: #36904a;
	  margin-right: 20rpx;
	}
	
	.title-text {
	  flex: 1;
	}
	
	.list {
	  background: #ffffff;
	  padding-left: 32rpx;
	}
	.list .list-item {
	  border-bottom: 1px solid #eaeaea;
	  display: flex;
	  align-items: center;
	  padding-top: 20rpx;
	  padding-bottom: 20rpx;
	  height: 89rpx;
	  line-height: 89rpx;
	}
	.getverifyCode {
	  font-size: 32rpx;
	  border: 0;
	  color: #36904a;
	  height: 89rpx;
	  line-height: 89rpx;
	}
	.list .list-item:last-child {
	  border: 0;
	}
	.list .left {
	  width: 196rpx;
	  font-size: 32rpx;
	}
	.list .right {
	  flex: 1;
	  height: 89rpx;
	  line-height: 89rpx;
	  padding: 0;
	}
	
	.letter-spacing {
	  letter-spacing: 16rpx;
	}
	
	.btnBox {
	  padding-top: 166rpx;
	}
	
	.getverifyCode {
		background: #FFFFFF;
		border-color: #ffffff;
	}
	
	button::after { border: none }
</style>
